﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Web.Mvc;
using ShopB2C.Entity;
using ShopB2C.Enums;
using ShopB2C.Helper;
using ShopB2C.Interfaces;
using ShopB2C.ViewModels;

namespace ShopB2C.Controllers.Manager {
    public partial class CommentController : BaseController {
        private static IComment _comment;
        private static IProductPreview _productPreView;

        public CommentController() {
            _comment = DataBaseExecuter.CreateClassByName<IComment>("Comment");
            _productPreView = DataBaseExecuter.CreateClassByName<IProductPreview>("ProductPreView");
        }

        #region Comment

        public ActionResult CommentProduct(
            [DefaultValue("a")]string audit,
            [DefaultValue(1)]int productPage) {
            this.SiteMapPathAdd("包含评论的商品");
            ViewData["Audit"] = audit;

            bool? auditB = null;
            if (!string.IsNullOrEmpty(audit) && audit != "a") {
                auditB = audit == "t" ? true : false;
            }

            int pageCount;
            return View(new ProductViewModel() {
                ProductList = _comment.GetCommentProduct(auditB, 10, productPage, out pageCount)
                    ?? new List<ProductEntity>(),
                ImageRoot = _imageRoot,
                PageNumber = productPage,
                Pager = this.CreatePager(Url.Action("CommentProduct", new {
                    productPage = "{0}",
                    audit = audit
                }), pageCount, productPage)
            });
        }

        public ActionResult CommentList(
            [DefaultValue("a")]string audit,
            [DefaultValue(1)]int productPage,
            [DefaultValue(0)]int productId,
            [DefaultValue(1)]int commentPage) {

            ViewData.Add("Audit", audit);
            ViewData.Add("ProductPage", productPage);
            ProductEntity product = _productPreView.ProductView(productId, 0);
            CommentParameters cps = new CommentParameters() {
                IsAudit = null,
                IsReply = CommentType.Comment,
                ProductId = productId,
                PageIndex = commentPage
            };
            List<CommentEntity> commentList = _comment.GetCommentList(cps) ?? new List<CommentEntity>();
            List<CommentEntity> replyList = _comment.GetCommentList(new CommentParameters() {
                IsReply = CommentType.Reply,
                ProductId = productId,
                IsAudit = null
            }) ?? new List<CommentEntity>();

            this.SiteMapPathAdd("包含评论的商品", Url.Action("CommentProduct", "Comment", new {
                audit = audit,
                productPage = productPage
            }));
            this.SiteMapPathAdd("商品评论");

            return View(new CommentViewModel() {
                Product = product,
                CommentList = commentList,
                ReCommentList = replyList,
                ImageRoot = _imageRoot,
                PageNumber = commentPage,
                Pager = this.CreatePager(Url.Action("CommentList", new {
                    audit = audit,
                    productId = productId,
                    productPage = productPage,
                    commentPage = "{0}"
                }), cps.PageCount, commentPage)
            });
        }

        public ActionResult CommentEdit(
            [DefaultValue("a")]string audit,
            [DefaultValue(1)]int productPage,
            [DefaultValue(0)]int productId,
            [DefaultValue(1)]int commentPage,
            [DefaultValue(0)]int commentId) {

            string url = Url.Action("CommentList", new {
                audit = audit,
                productId = productId,
                productPage = productPage,
                commentPage = commentPage
            });
            this.SiteMapPathAdd("包含评论的商品", Url.Action("CommentProduct", "Comment", new {
                audit = audit,
                productPage = productPage
            }));
            this.SiteMapPathAdd("商品评论", url);
            this.SiteMapPathAdd("商品评论编辑");

            ViewData.Add("SubmitUrl", Url.Action("CommentEdit", new {
                audit = audit,
                productId = productId,
                productPage = productPage,
                commentPage = commentPage
            }));
            ViewData.Add("ReturnUrl", url);
            return View(new CommentViewModel() {
                Comment = commentId == 0 ? new CommentEntity() {
                    GradeValue = 5,
                    Content = string.Empty
                } : _comment.GetCommentById(commentId),
                PageNumber = commentPage,
            });
        }

        [HttpPost]
        public ActionResult CommentEdit(
            [DefaultValue("a")]string audit,
            [DefaultValue(1)]int productPage,
            [DefaultValue(0)]int productId,
            [DefaultValue(1)]int commentPage,
            CommentViewModel cvm) {
            #region 检查数据合法性

            MessageHelper mh = new MessageHelper("comment");
            mh.AddBody(2);
            if (cvm.Comment.GradeValue < 1) {
                mh.AddMessage(3);
            }
            if (string.IsNullOrEmpty(cvm.Comment.Content)) {
                mh.AddMessage(4);
            }
            if (!mh.IsValid) {
                return this.MessageResult(mh.CreateMessage());
            }

            #endregion

            CommentEntity ce = _comment.GetCommentById(cvm.Comment.CommentId);
            ce.GradeValue = cvm.Comment.GradeValue;
            ce.Content = cvm.Comment.Content;
            _comment.CommentUpdate(ce);

            return RedirectToAction("CommentList", new {
                audit = audit,
                productId = productId,
                productPage = productPage,
                commentPage = commentPage
            });
        }

        public ActionResult CommentAudit(
            [DefaultValue("a")]string audit,
            [DefaultValue(1)]int productPage,
            [DefaultValue(0)]int productId,
            [DefaultValue(1)]int commentPage,
            string ids) {
            _comment.CommentAudit(ids);

            return RedirectToAction("CommentList", new {
                audit = audit,
                productId = productId,
                productPage = productPage,
                commentPage = commentPage
            });
        }

        public ActionResult CommentDelete(
            [DefaultValue("a")]string audit,
            [DefaultValue(1)]int productPage,
            [DefaultValue(0)]int productId,
            string ids) {
            _comment.CommentDelete(ids);

            return RedirectToAction("CommentList", new {
                audit = audit,
                productId = productId,
                productPage = productPage
            });
        }

        #endregion
    }
}
